# 821 简单：字符的最短距离
import numpy as np


def shortestToChar(s: str, c: str):
    ret = [0] * len(s)

    loc = [-np.inf]
    i = 0
    while i < len(s) and s.find(c, i) > -1:
        rt = s.find(c, i)
        loc.append(rt)
        i = rt + 1
    loc.append(np.inf)
    print(loc)

    j = 1
    left, right = loc[j - 1], loc[j]
    for i in range(len(s)):
        if s[i] == c:
            j += 1
            left = loc[j - 1]
            right = loc[j]
            continue
        ret[i] = min(i - left, right - i)

    return ret

    # class Solution:
    #     def shortestToChar(self, s: str, c: str) -> List[int]:
    #         n = len(s)
    #         ans = [0] * n
    #
    #         idx = -n
    #         for i, ch in enumerate(s):
    #             if ch == c:
    #                 idx = i
    #             ans[i] = i - idx
    #
    #         idx = 2 * n
    #         for i in range(n - 1, -1, -1):
    #             if s[i] == c:
    #                 idx = i
    #             ans[i] = min(ans[i], idx - i)
    #         return ans


print(shortestToChar("loveleetcode", 'c'))
